//
//  LoginViewController.m
//  O2O Product
//
//  Created by 特沃兹 on 2017/12/29.
//  Copyright © 2017年 Product Author. All rights reserved.
//

#import "LoginViewController.h"

@interface LoginViewController ()

@end

@implementation LoginViewController
#pragma mark - ---------- Lazy Loading（懒加载） ----------
-(UIButton *)LoginButton{
    if (_LoginButton == nil) {
        _LoginButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_LoginButton setBackgroundColor:Color_d73b3c];
        [_LoginButton setTitle:@"登录" forState:UIControlStateNormal];
        [_LoginButton setTitleColor:Color_ffffff forState:UIControlStateNormal];
        [_LoginButton.layer setCornerRadius:2];
        [_LoginButton addTarget:self action:@selector(LoginAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _LoginButton;
}
-(UIButton *)RegisteredButton{
    if (_RegisteredButton == nil) {
        _RegisteredButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_RegisteredButton setTitle:@"注册" forState:UIControlStateNormal];
        [_RegisteredButton setTitleColor:Color_999999 forState:UIControlStateNormal];
        [_RegisteredButton addTarget:self action:@selector(RegisteredAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _RegisteredButton;
}
-(UIButton *)ForgotPasswordButton{
    if (_ForgotPasswordButton == nil) {
        _ForgotPasswordButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_ForgotPasswordButton setTitle:@"忘记密码?" forState:UIControlStateNormal];
        [_ForgotPasswordButton setTitleColor:Color_999999 forState:UIControlStateNormal];
        [_ForgotPasswordButton addTarget:self action:@selector(ForgotPasswordAction) forControlEvents:UIControlEventTouchUpInside];

    }
    return _ForgotPasswordButton;
}
-(UIButton *)ValidationButton{
    if (_ValidationButton == nil) {
        _ValidationButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_ValidationButton setBackgroundColor:[ChangeColor hexStringToColor:@"f0f0f0"]];
        [_ValidationButton setTitle:@"验证码" forState:UIControlStateNormal];
        [_ValidationButton setTitleColor:Color_999999 forState:UIControlStateNormal];
        [_ValidationButton.layer setCornerRadius:2];
        [_ValidationButton.layer setBorderWidth:1];
        [_ValidationButton.layer setBorderColor:[ChangeColor hexStringToColor:@"d2d2d2"].CGColor];
        [_ValidationButton addTarget:self action:@selector(ValidationAction) forControlEvents:UIControlEventTouchUpInside];

    }
    return _ValidationButton;
}
-(TextFieldView *)PhoneText
{
    if (_PhoneText == nil) {
        _PhoneText = [[TextFieldView alloc]initWithFrame:CGRectMake(20, 50+TopBarHeight, Main_Screen_Width-40, 50)];
        _PhoneText.backgroundColor = [UIColor clearColor];
        _PhoneText.layer.borderWidth = 1;
        _PhoneText.layer.borderColor = [ChangeColor hexStringToColor:@"d2d2d2"].CGColor;
        [_PhoneText.leftIcon setImage:[UIImage imageNamed:@"Login_Phone"] forState:UIControlStateNormal];
        _PhoneText.placeholderString = @"请输入手机号码";
        _PhoneText.maxTextLength = 11;
        _PhoneText.layer.cornerRadius = 2;
        _PhoneText.TextF.keyboardType = UIKeyboardTypePhonePad;
    }
    return _PhoneText;
}
-(TextFieldView *)PasswordText
{
    if (_PasswordText == nil) {
        _PasswordText = [[TextFieldView alloc]initWithFrame:CGRectMake(20, 50+TopBarHeight, Main_Screen_Width-40, 50)];
        _PasswordText.backgroundColor = [UIColor clearColor];
        _PasswordText.layer.borderWidth = 1;
        _PasswordText.layer.borderColor = [ChangeColor hexStringToColor:@"d2d2d2"].CGColor;
        [_PasswordText.leftIcon setImage:[UIImage imageNamed:@"Login_Pass"] forState:UIControlStateNormal];
        _PasswordText.placeholderString = @"请输入6-12位英文字符及数字";
        _PasswordText.maxTextLength = 12;
        _PasswordText.layer.cornerRadius = 2;
        _PasswordText.TextF.keyboardType = UIKeyboardTypePhonePad;
    }
    return _PasswordText;
}
-(TextFieldView *)ValidationText
{
    if (_ValidationText == nil) {
        _ValidationText = [[TextFieldView alloc]initWithFrame:CGRectMake(20, 50+TopBarHeight, Main_Screen_Width-100, 50)];
        _ValidationText.backgroundColor = [UIColor clearColor];
        _ValidationText.layer.borderWidth = 1;
        _ValidationText.layer.borderColor = [ChangeColor hexStringToColor:@"d2d2d2"].CGColor;
        [_ValidationText.leftIcon setImage:[UIImage imageNamed:@"Login_Validation"] forState:UIControlStateNormal];
        _ValidationText.placeholderString = @"请输入验证码";
        _ValidationText.maxTextLength = 6;
        _ValidationText.layer.cornerRadius = 2;
        _ValidationText.TextF.keyboardType = UIKeyboardTypePhonePad;
    }
    return _ValidationText;
}
-(UIButton *)AssociateButton{
    if (_AssociateButton == nil) {
        _AssociateButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_AssociateButton.layer setCornerRadius:30];
        [_AssociateButton.layer setMasksToBounds:YES];
        [_AssociateButton setImage:[UIImage imageNamed:@"wechat-pic"] forState:UIControlStateNormal];
        [_AssociateButton addTarget:self action:@selector(AssociateAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _AssociateButton;
}
#pragma mark - ----------   Lifecycle（生命周期） ----------
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.view  addSubview:self.PhoneText];
    [self InitSubView];
}
-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
}
-(void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
}
#pragma mark - ---------- Private Methods（私有方法） ----------

#pragma mark initliaze data(初始化数据)

#pragma mark config control（布局控件）
-(void)InitSubView{
    [self.PhoneText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_offset(0);
        make.size.mas_offset(CGSizeMake(Main_Screen_Width-40, 50));
        make.top.mas_offset(TopBarHeight+50);
    }];
    [self.view  addSubview:self.PasswordText];
    [self.PasswordText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_offset(0);
        make.top.equalTo(self.PhoneText.mas_bottom).offset(10);
        make.size.mas_offset(CGSizeMake(Main_Screen_Width-40, 50));
    }];
    [self.view addSubview:self.ValidationButton];
    [self.ValidationButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.PasswordText.mas_bottom).offset(10);
        make.right.equalTo(self.PasswordText.mas_right).offset(0);
        make.size.mas_offset(CGSizeMake(194/2, 50));
    }];
    [self.view  addSubview:self.ValidationText];
    [self.ValidationText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.PasswordText.mas_bottom).offset(10);
        make.left.equalTo(self.PasswordText.mas_left).offset(0);
        make.right.equalTo(self.ValidationButton.mas_left).offset(-10);
        make.bottom.equalTo(self.ValidationButton.mas_bottom).offset(0);
    }];
    
    [self.view addSubview:self.LoginButton];
    [self.LoginButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_offset(0);
        make.top.equalTo(self.ValidationText.mas_bottom).offset(10);
        make.size.mas_offset(CGSizeMake(Main_Screen_Width-40, 50));
    }];
    [self.view addSubview:self.RegisteredButton];
    [self.RegisteredButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.LoginButton.mas_bottom).offset(10);
        make.left.equalTo(self.LoginButton.mas_left).offset(0);
        make.size.mas_offset(CGSizeMake(40, 40));
    }];
    [self.view addSubview:self.ForgotPasswordButton];
    [self.ForgotPasswordButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.LoginButton.mas_bottom).offset(10);
        make.right.equalTo(self.LoginButton.mas_right).offset(0);
        make.size.mas_offset(CGSizeMake(90, 40));
    }];
    //关联账户
    [self.view addSubview:self.AssociateButton];
    [self.AssociateButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_offset(-50);
        make.centerX.mas_offset(0);
        make.size.mas_offset(CGSizeMake(60, 60));
    }];
}
#pragma mark networkRequest (网络请求)



#pragma mark IBActions （点击事件xib）
-(void)ReturnViewAction{
    /*返回上一页*/
    
}
-(void)RegisteredAction{
    /*进入注册页*/
    DLog(@"进入注册页");
    [self presentViewController:[RegisteredViewController new] animated:YES completion:nil];
}
//登陆
-(void)LoginAction:(UIButton *)button{
    /*手机号、密码、验证码（若有）为空的时候呈灰态（按钮仍可点击），验证分无验证码和有验证码两种情况：
     1.无验证码时点击按钮时先校验手机及密码输入是否为空，错误飘字“请输入手机号或密码”；
     其次校验手机号及密码是否正确，错误或手机号未注册飘字“手机号或密码不正确”；
     
     2.有验证码则先校验手机及密码输入是否为空，错误飘字“请输入手机号或密码”；其次校验验证码是否为空，错误飘字“请输入验证码”；
     然后校验验证码是否正确，错误飘字“验证码错误”；过期飘字“验证码已过期，请重新输入验证码”，并重新刷新验证码；
     最后校验手机号及密码是否正确，错误或手机号未注册飘字“手机号或密码不正确”；
     5.手机密码均正确则跳转至来源页。特别的，从个人中心点击登录的用户跳转至首页；
     6.当帐号被禁用时飘字“该帐号已被禁用”*/
    DLog(@"登陆");

}

-(void)PasswordClearAction{
    /*可以明文查看已输入的密码*/
}
//验证码
-(void)ValidationAction{
    /*
     1.输错三次密码以上弹出验证码重新请求接口，之后每次输错验证码均刷新；
     3.验证码图片点击刷新；
     2.验证码有效期5分钟
     */
    DLog(@"验证码图片点击刷新");

}
-(void)ForgotPasswordAction{
    /*进入忘记密码页*/
    DLog(@"进入忘记密码页");
    [self presentViewController:[BackPassword_OneViewController new] animated:YES completion:nil];
}
-(void)AssociateAction{
    /*进入关联账户页*/
    [self presentViewController:[Associate_OneViewController new] animated:YES completion:nil];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
